package com.current;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 项目名称: java_algorithm
 * @ClassName CompletableFutureTest
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2022/5/11 14:11
 */
public class CompletableFutureTest {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //1、有返回值
//        CompletableFuture.runAsync(()-> System.out.println("thread1"));
//        CompletableFuture.runAsync(()-> System.out.println("thread2"));
//        Thread.sleep(1000);
//        System.out.println("完成");

        //2、无返回值
//        System.out.println(CompletableFuture.supplyAsync(()-> "thread1").get());
//        System.out.println(CompletableFuture.supplyAsync(()-> "thread2").get());
//        System.out.println("完成");

        //3、前面的返回值给后面作为参数使用  thenAccept没有返回值 supplyAsync有返回值
//        CompletableFuture.supplyAsync(()->"thread1").thenAccept(x-> System.out.println(x));
//        System.out.println(CompletableFuture.supplyAsync(()->"thread1").thenApply(x-> x).get());
//        System.out.println("完成");

        //4、前面2个任务执行返回的结果利用thenCombine方法进行数据重新组装
//        CompletableFuture<Integer> integerCompletableFuture1 = CompletableFuture.supplyAsync(() -> 1);
//        CompletableFuture<Integer> integerCompletableFuture2 = CompletableFuture.supplyAsync(() -> 2);
//        System.out.println(integerCompletableFuture1.thenCombine(integerCompletableFuture2,(result1,result2)-> result1+result2).get());
//        System.out.println("完成");

        //5、前面四个任务全部执行完了之后才能接触join方法的阻塞
//        CompletableFuture<Void> thread1 = CompletableFuture.runAsync(() -> System.out.println("thread1"));
//        CompletableFuture<Void> thread11 = CompletableFuture.runAsync(() -> System.out.println("thread2"));
//        CompletableFuture<Void> thread12 = CompletableFuture.runAsync(() -> System.out.println("thread3"));
//        CompletableFuture<Void> thread13 = CompletableFuture.runAsync(() -> System.out.println("thread4"));
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(thread1, thread11, thread12, thread13);
//        voidCompletableFuture.join();
//        System.out.println("完成");

        //6、前面四个任务全部执行完了之后才能接触join方法的阻塞
        CompletableFuture<Void> thread1 = CompletableFuture.runAsync(() -> System.out.println("thread1"));
        CompletableFuture<Void> thread11 = CompletableFuture.runAsync(() -> System.out.println("thread2"));
        CompletableFuture<Void> thread12 = CompletableFuture.runAsync(() -> System.out.println("thread3"));
        CompletableFuture<Void> thread13 = CompletableFuture.runAsync(() -> System.out.println("thread4"));
        CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(thread1, thread11, thread12, thread13);
        objectCompletableFuture.join();
        System.out.println("完成");
    }
}
